Imports System.Xml.Serialization
Public Class GuessClassName
    Private Const XmlFilename As String = "PlayersSpellAndClass.xml"

    Public Enum ClassIndex As Integer
        Unknown
        Any
        Warrior
        Paladin
        Hunter
        Rogue
        Priest
        Shaman
        Mage
        Warlock
        Druid
        DeathKnight
    End Enum

    Public ClassNames() As String = {"Unknown", "Any", "Warrior", "Paladin", "Hunter", "Rogue", "Priest", "Shaman", "Mage", "Warlock", "Druid", "Death Knight"}
    Public ClassNamesShort() As String = {"unk", "any", "war", "pal", "hun", "rog", "pri", "sha", "mag", "lck", "drd", "dkn"}

    Public Structure SpellEntry
        <XmlAttribute()> Dim spell As String
        <XmlAttribute()> Dim spellId As Integer
        '<XmlAttribute()> Dim eventName As String

        <XmlAttribute()> Dim classIdx As ClassIndex
        <XmlAttribute()> Dim guessIdx As ClassIndex
        <XmlAttribute()> Dim build As String
    End Structure

    Public PlayersSpells() As SpellEntry


    Public Sub New()
        Dim XU As New XmlUtils

        ReDim PlayersSpells(0)
        PlayersSpells(0) = New SpellEntry
        PlayersSpells(0).spell = "Unknown"

        Try
            XU.ImportFromXml(PlayersSpells, getXmlFilename(XmlFilename))
        Catch ex As Exception
            Dim EH As New ErrorHandler
            EH.ErrorMsgBox(ex, "ClassName.New ImportFromXml")
        End Try
    End Sub

    Protected Overrides Sub Finalize()
        Dim XU As New XmlUtils
        Try
            XU.ExportByXml(PlayersSpells, getXmlFilename(XmlFilename))
        Catch ex As Exception
            Dim EH As New ErrorHandler
            EH.ErrorMsgBox(ex, "ClassName.Finalize ExportByXml")
        End Try

        MyBase.Finalize()
    End Sub

    Private Function getXmlFilename(ByVal filename As String) As String
        Dim path As String = My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
        Return My.Computer.FileSystem.CombinePath(path, filename)
    End Function

    Public Function GuessClassName(ByVal SpellName As String, ByVal SpellID As Integer, Optional ByVal ClassIdx As ClassIndex = ClassIndex.Unknown) As ClassIndex
        Dim i As Integer
        Dim EH As New ErrorHandler

        For i = 0 To PlayersSpells.Length - 1
            If PlayersSpells(i).spellId = SpellID Then
                '--- update guess class index
                'If ClassIdx <> ClassIndex.Unknown Then
                If PlayersSpells(i).guessIdx = ClassIndex.Any Then
                    '
                ElseIf PlayersSpells(i).guessIdx = ClassIndex.Unknown Then
                    PlayersSpells(i).guessIdx = ClassIdx
                ElseIf PlayersSpells(i).guessIdx <> ClassIdx Then
                    PlayersSpells(i).guessIdx = ClassIndex.Any
                End If

                ' End If

                If PlayersSpells(i).classIdx <> ClassIndex.Unknown And PlayersSpells(i).guessIdx <> ClassIndex.Unknown And PlayersSpells(i).classIdx <> PlayersSpells(i).guessIdx Then
                    EH.ErrorLog("GuessClassName", "GuessClassName", "Class is not certain", "Spell=" & SpellName & " Class=" & ClassNames(PlayersSpells(i).classIdx))
                End If

                If PlayersSpells(i).classIdx <> ClassIndex.Unknown Then
                    Return PlayersSpells(i).classIdx
                Else
                    Return PlayersSpells(i).guessIdx
                End If
            End If
        Next

        If PlayersSpells.Length = 1 And PlayersSpells(0).spell = "Unknown" Then
            '
        Else
            ReDim Preserve PlayersSpells(PlayersSpells.Length)
        End If
        PlayersSpells(PlayersSpells.Length - 1) = New SpellEntry
        PlayersSpells(PlayersSpells.Length - 1).classIdx = ClassIndex.Unknown
        PlayersSpells(PlayersSpells.Length - 1).spellId = SpellID
        PlayersSpells(PlayersSpells.Length - 1).spell = SpellName
        'PlayersSpells(PlayersSpells.Length - 1).eventName = EventName

        EH.ErrorLog("GuessClassName", "GuessClassName", "Added Unknown Class", "Spell=" & SpellName)

        Return ClassIndex.Unknown
    End Function

    Public Function GetClassIndex(ByVal ClassName As String) As ClassIndex
        Dim i As Integer

        For i = 0 To ClassNames.Length - 1
            If ClassNames(i) = ClassName Then
                Return i
            End If
        Next

        Return ClassIndex.Unknown
    End Function
End Class

